/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.mathematica.omparser;

/**
 *
 * @author Mr.Sam
 */
import java.io.*;
import java.util.*;
import fr.inria.openmath.omapi.*;

/**
* Handler for an OpenMath parser. Keeps a copy of the object parsed.
*
*/
public class PipeParserHandler extends ParserAdapter {
    private Stack stackIndentation = new Stack();
    private StringBuffer omObjectParsed = new StringBuffer();
    private String indentation = "";

    /**
    * Returns the OpenMath object parsed.
    */
    public String getOmObjectParsed() {
        return omObjectParsed.toString();
    }

    /**
    * Throws an OpenMath exception.
    * @param message the message to display.
    * @param e a specific exception to throw.
    * @see fr.inria.openmath.omapi.OMException
    */
    private void throwOME(String message, Exception e) throws OMException {
        throw new OMException(message, e);
    }

    /**
    * Throws an OpenMath exception.
    * @param message the message to display.
    * @param code the code of the excpetion thrown.
    * @see fr.inria.openmath.omapi.OMException
    */
    private void throwOME(String message, short code) throws OMException {
        throw new OMException(message, code);
    }

    /**
    * Throws an OpenMath exception.
    * @param message the message to display.
    * @see fr.inria.openmath.omapi.OMException
    */
    private void throwOME(String message) throws OMException {
        throwOME(message, OMException.OME_GENERIC);
    }


    /*******************************************/
    /*                                         */
    /* Les méthodes de la classe ParserAdapter */
    /*                                         */
    /*******************************************/
    
    
    /**
     * Receives notification of the end of parse.
     *
     * <p>By default, do nothing. Application writers may override this
     * method in a subclass.</p>
     *
     * <p>The OpenMath parser will invoke this method only once, and it will
     * be the last method invoked during the parse.  The parser shall
     * not invoke this method until it has either abandoned parsing
     * (because of an unrecoverable error) or reached the end of
     * the input (no more OpenMath objects).</p>
     *
     * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
     */
    public void endParse() throws OMException {
        omObjectParsed.setLength(0);
    }

    /**
     * Receives notification of the beginning of a object element.
     *
     * <p>By default, do nothing.  Application writers may override this
     * method in a subclass.</p>
     *
     * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
     * @see #endObject
     */
    public void startObject() throws OMException {
        stackIndentation.removeAllElements();
        omObjectParsed.setLength(0);
        indentation = "";

        omObjectParsed.append("<OMOBJ>\n");
        stackIndentation.push(indentation);
        indentation += "  ";
    }

    /**
     * Receives notification of the end of an object element.
     *
     * <p>By default, do nothing.  Application writers may override this
     * method in a subclass.</p>
     *
     * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
     * @see #startObject
     */
    public void endObject() throws OMException {
        indentation = (String) stackIndentation.pop();
        omObjectParsed.append(indentation + "</OMOBJ>");
    }

    /**
     * Receives notification of the beginning of a application element.
     *
     * <p>By default, do nothing.  Application writers may override this
     * method in a subclass.</p>
     *
     * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
     * @see #endApplication
     */
    public void startApplication() throws OMException {
        omObjectParsed.append(indentation + "<OMA>\n");
        stackIndentation.push(indentation);
        indentation += "  ";
    }

    /**
     * Receives notification of the end of an application element.
     *
     * <p>By default, do nothing. Application writers may override this
     * method in a subclass.</p>
     *
     * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
     * @see #startApplication
     */
    public void endApplication() throws OMException {
        indentation = (String) stackIndentation.pop();
        omObjectParsed.append(indentation + "</OMA>\n");
    }


    /**
     * Receives notification of the beginning of a attribution element.
     *
     * <p>By default, do nothing.  Application writers may override this
     * method in a subclass.</p>
     *
     * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
     * @see #endAttribution
     */
    public void startAttribution() throws OMException {
        omObjectParsed.append(indentation + "<OMATTR>\n");
        stackIndentation.push(indentation);
        indentation += "  ";
    }

    /**
     * Receives notification of the end of an attribution element.
     *
     * <p>By default, do nothing.  Application writers may override this
     * method in a subclass.</p>
     *
     * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
     * @see #startAttribution
     */
    public void endAttribution() throws OMException {
        indentation = (String) stackIndentation.pop();
        omObjectParsed.append(indentation + "</OMATTR>\n");
    }

    /**
     * Receives notification of the beginning of an attribute-pairs element.
     *
     * <p>By default, do nothing.  Application writers may override this
     * method in a subclass.</p>
     *
     * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
     *
     * @see #endAttributePairs
     */
    public void startAttributePairs() throws OMException {
        omObjectParsed.append(indentation + "<OMATP>\n");
        stackIndentation.push(indentation);
        indentation += "  ";
    }

    /**
     * Receives notification of the end of an attribute-pairs element.
     *
     * <p>By default, do nothing.  Application writers may override this
     * method in a subclass.</p>
     *
     * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
     * @see #startAttribution
     */
    public void endAttributePairs() throws OMException {
        indentation = (String) stackIndentation.pop();
        omObjectParsed.append(indentation + "</OMATP>\n");
    }

    /**
     * Receives notification of the beginning of a Bind element.
     *
     * <p>By default, do nothing.  Application writers may override this
     * method in a subclass.</p>
     *
     * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
     *
     * @see #endBind
     */
    public void startBind() throws OMException {
        omObjectParsed.append(indentation + "<OMBIND>\n");
        stackIndentation.push(indentation);
        indentation += "  ";
    }

    /**
     * Receives notification of the beginning of a BVars element.
     *
     * <p>By default, do nothing.  Application writers may override this
     * method in a subclass.</p>
     *
     * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
     *
     * @see #endBVars
     */
    public void startBVars() throws OMException {
        omObjectParsed.append(indentation + "<OMBVAR>\n");
        stackIndentation.push(indentation);
        indentation += "  ";
    }

    /**
     * Receives notification of the beginning of a BVars element.
     *
     * <p>By default, do nothing.  Application writers may override this
     * method in a subclass.</p>
     *
     * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
     *
     * @see #startBVars
     */
    public void endBVars() throws OMException {
        indentation = (String) stackIndentation.pop();
        omObjectParsed.append(indentation + "</OMBVAR>\n");
    }


    /**
     * Receives notification of the beginning of a Bind element.
     *
     * <p>By default, do nothing.  Application writers may override this
     * method in a subclass.</p>
     *
     * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
     *
     * @see #endBind
     */
    public void endBind() throws OMException {
        indentation = (String) stackIndentation.pop();
        omObjectParsed.append(indentation + "</OMBIND>\n");
    }

    /**
     * Receives notification of the beginning of a error element.
     *
     * <p>By default, do nothing.  Application writers may override this
     * method in a subclass.</p>
     *
     * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
     *
     * @see #endError
     */
    public void startError() throws OMException {
        omObjectParsed.append(indentation + "<OME>\n");
        stackIndentation.push(indentation);
        indentation += "  ";
    }

    /**
     * Receives notification of the end of an error element.
     *
     * <p>By default, do nothing.  Application writers may override this
     * method in a subclass.</p>
     *
     * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
     * @see #startError
     */
    public void endError() throws OMException {
        indentation = (String) stackIndentation.pop();
        omObjectParsed.append(indentation + "</OME>\n");
    }


    /**
     * Receives notification of an Integer element.
     *
     * <p>By default, do nothing. Application writers may override this
     * method in a subclass.</p>
     *
     * @param value the value embeded in this Integer element. This is an infinite precision integer.
     * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
     * @see fr.inria.openmath.omapi.BigInt
     */
    public void readInteger(BigInt value) throws OMException {
        String strValue = (value.getSign() == 1) ? value.getDigits() : "-" + value.getDigits();
        omObjectParsed.append(indentation + "<OMI>" + strValue + "</OMI>\n");
    }

    /**
     * Receives notification of a floating-point number element.
     *
     * <p>By default, do nothing.  Application writers may override this
     * method in a subclass.</p>
     *
     * @param value the value embeded in this Float element.
     * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
     *
     */
    public void readFloat(double value) throws OMException {
        omObjectParsed.append(indentation + "<OMF dec = \"" + value + " \"/>\n");
    }

    /**
     * Receives notification of a string element.
     *
     * <p>By default, do nothing.  Application writers may override this
     * method in a subclass.</p>
     *
     * @param value the value embeded in this String element.
     * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
     *
     */
    public void readString(String value) throws OMException {
        omObjectParsed.append(indentation + "<OMSTR>" + value + "</OMSTR>\n");
    }

    /**
     * Receives notification of a variable element.
     *
     * <p>By default, do nothing.  Application writers may override this
     * method in a subclass.</p>
     *
     * @param name the name of this Variable element.
     * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
     *
     */
    public void readVariable(String name) throws OMException {
        omObjectParsed.append(indentation + "<OMV name=\""+ name +"\"/> \n");
    }

    /**
     * Receives notification of a byteArray element.
     *
     * <p>By default, do nothing.  Application writers may override this
     * method in a subclass.</p>
     *
     * @param value the value embeded in this ByteArray element.
     * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
     *
     */
    public void readByteArray(byte[] value) throws OMException {
    }

    /**
     * Receives notification of a symbol element.
     *
     * <p>By default, do nothing.  Application writers may override this
     * method in a subclass.</p>
     *
     * @param value the value embeded in this Symbol element.
     * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
     * @see Symbol
     */
    public void readSymbol(Symbol value) throws OMException {
        omObjectParsed.append(indentation + "<OMS cd=\""+value.getCD()+"\" name=\""+value.getName()+"\"/> \n");
    }

    /**
     * Receives notification of a comment element.
     *
     * <p>By default, do nothing.  Application writers may override this
     * method in a subclass.</p>
     *
     * @param value the value embeded in this Comment element.
     * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
     * @see Comment
     */
    public void readComment(String value) throws OMException {}
}
